package projectx.web.base;

import game.web.base.UserGroup;

import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import projectx.core.base.ProjectXContext;
import projectx.core.base.ProjectXLog;
import projectx.core.base.ProjectXMessage;
import projectx.core.base.ProjectXProperties;
import projectx.db.ProjectXDataSource;


public class ProjectXPlugIn implements PlugIn {

    //--------------------------------------------------------------------------
    //   S T A T I C   M E M B E R S
    //
    //--------------------------------------------------------------------------
    private static final Logger _log = Logger.getLogger( ProjectXPlugIn.class.getName() );


    //--------------------------------------------------------------------------
    //   M E M B E R S
    //
    //--------------------------------------------------------------------------
    private String  _log4jFileName;
    private String  _propFileName;
    private String  _msgFileName;
    private String  _dataSourceName;

    //--------------------------------------------------------------------------
    //   M E T H O D S
    //
    //--------------------------------------------------------------------------

    public void setLog4jFileName( String log4jFileName ){ _log4jFileName = log4jFileName; }


    public void setPropFileName( String propFileName ){ _propFileName = propFileName; }

  
    public void setMsgFileName( String msgFileName ){ _msgFileName = msgFileName; }

    public void setDataSourceName( String dataSourceName ){ _dataSourceName = dataSourceName; }

    public void init( ActionServlet servlet, ModuleConfig config ) throws ServletException {
    	 try {

            ProjectXLog.initialize( _log4jFileName );

            _log.info( "Initialize ProjectX ..." );

            ProjectXProperties.initialize( _propFileName   );
            ProjectXMessage   .initialize( _msgFileName    );
            ProjectXContext   .initialize();
            ProjectXDataSource.initialize( _dataSourceName );
            UserGroup        .initialize();

            _log.info( "Initialized successfully" );
        }
        catch ( Throwable e ){
            _log.fatal( "Failed in initialize", e );
            throw new ServletException( e );
        }
    }

    public void destroy(){

        _log.info( "Shutdown ProjectX ..." );

        ProjectXLog.shutdown();
    }
}
